package es.deusto.ingenieria.tesoroaventurero;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import es.deusto.ingenieria.is.xml.InformationXMLReader;

public class EscenarioXMLReader extends InformationXMLReader {

	private Casilla tesoro;
	private Casilla personaje;
	private Casilla	[][] celdas;
	
	public EscenarioXMLReader(String fileXML) {
		super(fileXML);
	}

	
	public Object getInformation(){
		Escenario escenario = new Escenario(this.celdas, this.tesoro.x, this.tesoro.y);
		escenario.getPersonaje().setLocation(this.personaje);
		return escenario;
	}
	
	
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		try{
			if(qName.equals("is:maze")){
				this.celdas = new Casilla[Integer.parseInt(attributes.getValue("height"))][Integer.parseInt(attributes.getValue("width"))];
				
				for (int y=0; y<this.celdas.length; y++) {
					for (int x=0; x<this.celdas[y].length; x++) {
						this.celdas[y][x] = new Casilla();
						this.celdas[y][x].y = y;
						this.celdas[y][x].x = x;
					}
				}
			}else if (qName.equals("is:start")){				
				this.personaje = new Casilla();
				this.personaje.y = Integer.parseInt(attributes.getValue("row"))-1;
				this.personaje.x = Integer.parseInt(attributes.getValue("column"))-1;
			}else if (qName.equals("is:treasure")) {
				this.tesoro = new Casilla();
				this.tesoro.y = Integer.parseInt(attributes.getValue("row"))-1;
				this.tesoro.x = Integer.parseInt(attributes.getValue("column"))-1;
			}else if (qName.equals("is:gemlocation")) {
				this.celdas[Integer.parseInt(attributes.getValue("row"))-1][Integer.parseInt(attributes.getValue("column"))-1].setGema(true);
			}else if (qName.equals("is:eastarrow")) {
				this.celdas[Integer.parseInt(attributes.getValue("row"))-1][Integer.parseInt(attributes.getValue("column"))-1].setDerecha(true);
			}else if (qName.equals("is:westarrow")) {
				this.celdas[Integer.parseInt(attributes.getValue("row"))-1][Integer.parseInt(attributes.getValue("column"))-1].setIzquierda(true);
			}else if (qName.equals("is:northarrow")) {
				this.celdas[Integer.parseInt(attributes.getValue("row"))-1][Integer.parseInt(attributes.getValue("column"))-1].setArriba(true);
			}else if (qName.equals("is:southarrow")) {
				this.celdas[Integer.parseInt(attributes.getValue("row"))-1][Integer.parseInt(attributes.getValue("column"))-1].setAbajo(true);
			}
			
		}catch (Exception ex) {
			System.out.println(this.getClass().getName() + ".startElement(): " + ex);
			ex.printStackTrace();
		}
	}

}
